Public Class GDALColorTable

    Private obj As Long
    Private owned As Long
    Public EntryCount As Long
    Public PaletteInterpretation As Long

    ' ----------------------------------------------------------------------------
    Public Sub New()
        obj = 0
        owned = 0
        EntryCount = 0
        PaletteInterpretation = 0
    End Sub

    ' ----------------------------------------------------------------------------
    'Private Sub Finalize()
    '    If obj <> 0 And owned <> 0 Then
    '        Call Destroy()
    '    End If
    'End Sub

    ' ----------------------------------------------------------------------------
    Public Sub CInit(ByVal obj_in As Long, ByVal owned_in As Long)
        If obj <> 0 And owned <> 0 Then
            Call Destroy()
        End If
        obj = obj_in
        owned = owned_in
        If obj <> 0 Then
            EntryCount = GDALCore.GDALGetColorEntryCount(obj)
            PaletteInterpretation = GDALCore.GDALGetPaletteInterpretation(obj)
        End If
    End Sub

    ' ----------------------------------------------------------------------------
    Public Sub Destroy()
        If obj <> 0 Then
            'Call MsgBox( "Destroying ColorTable" )
            Call GDALCore.GDALDestroyColorTable(obj)
            obj = 0
            owned = 0
        End If
    End Sub

    ' ----------------------------------------------------------------------------
    Public Function GetObjPtr() As Long
        GetObjPtr = obj
    End Function

    ' ----------------------------------------------------------------------------
    Public Function IsValid() As Boolean
        If obj = 0 Then
            IsValid = False
        Else
            IsValid = True
        End If
    End Function

    ' ----------------------------------------------------------------------------
    Public Function Clone()
        Dim other_c_obj As Long
        Dim other_ct As GDALColorTable

        If obj <> 0 Then
            other_c_obj = GDALCore.GDALCloneColorTable(obj)
            other_ct = New GDALColorTable
            Call other_ct.CInit(other_c_obj, 1)
            Clone = other_ct
        End If
    End Function
    ' ----------------------------------------------------------------------------
    Public Sub SetColorEntry(ByVal ColorIndex As Long, _
    ByVal ColorEntry() As Integer)

        If obj <> 0 Then
            Dim SafeColorEntry(4) As Integer
            SafeColorEntry(0) = ColorEntry(0)
            SafeColorEntry(1) = ColorEntry(1)
            SafeColorEntry(2) = ColorEntry(2)
            SafeColorEntry(3) = ColorEntry(3)

            Call GDALCore.GDALSetColorEntry(obj, ColorIndex, SafeColorEntry(0))
        End If
    End Sub

    ' ----------------------------------------------------------------------------
    Public Function GetColorEntry(ByVal ColorIndex As Long, ByVal ColorEntry() As Integer) As Long

        If obj <> 0 Then
            Dim SafeColorEntry(4) As Integer
            GetColorEntry = GDALCore.GDALGetColorEntry(obj, ColorIndex, _
                                                        SafeColorEntry(0))
            ColorEntry(0) = SafeColorEntry(0)
            ColorEntry(1) = SafeColorEntry(1)
            ColorEntry(2) = SafeColorEntry(2)
            ColorEntry(3) = SafeColorEntry(3)
        Else
            GetColorEntry = GDALCore.ObjIsNULLError
        End If
    End Function

    ' ----------------------------------------------------------------------------
    Public Function GetColorEntryAsRGB(ByVal ColorIndex As Long, _
    ByVal ColorEntry() As Integer) As Long

        If obj <> 0 Then
            Dim SafeColorEntry(4) As Integer

            GetColorEntryAsRGB = GDALCore.GDALGetColorEntryAsRGB( _
                    obj, ColorIndex, SafeColorEntry(0))
            ColorEntry(0) = SafeColorEntry(0)
            ColorEntry(1) = SafeColorEntry(1)
            ColorEntry(2) = SafeColorEntry(2)
            ColorEntry(3) = SafeColorEntry(3)
        Else
            GetColorEntryAsRGB = GDALCore.ObjIsNULLError
        End If
    End Function
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        If obj <> 0 And owned <> 0 Then
            Call Destroy()
        End If
    End Sub

End Class
